﻿using AutoMapper;
using MediatR;
using Traceability.APP.Applications.Commands.Suyuan;
using Traceability.APP.Dtos;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.APP.Applications.CommandHandlers.Suyuan
{
    public class SuyuanCreateCommandHandler : IRequestHandler<SuyuanCreateCommand, APIResult<object>>
    {
        private readonly IBaseRepository<BasicTraceabilityModel> _syRepository;
        private readonly IMapper _mapper;

        public SuyuanCreateCommandHandler(IBaseRepository<BasicTraceabilityModel> syRepository, IMapper mapper)
        {
            _syRepository = syRepository;
            _mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(SuyuanCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.AddSuccess;
            result.Message = APIEnum.AddSuccess.Desc();
            BasicTraceabilityModel sy = _mapper.Map<BasicTraceabilityModel>(request);
            sy.CreateName = "张三";
            sy.CreateTime = DateTime.Now;
            sy.Information = 0;
            await _syRepository.Add(sy);
            return await Task.FromResult(result);
        }
    }
}
